{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 独立使用django model（一）\n",
    "# Django Shell\n",
    "# 项目根目录中打开Django Shell : python manage.py shell\n",
    "# import django\n",
    "# django.setup()  # 装载Django\n",
    "\n",
    "# 独立使用django model（二）\n",
    "# python ×××.py or Jupyter Notebook\n",
    "# 将脚本放置在项目根目录下jupyter_notebook文件中比较方便\n",
    "import sys\n",
    "import os\n",
    "import json\n",
    "import requests\n",
    "from django.core.files import File\n",
    "from django.core.files.base import ContentFile\n",
    "# 外部脚本链接django项目\n",
    "# 添加环境变量\n",
    "'''\n",
    "print(os.path.abspath('__file__'))\n",
    "print(os.path.dirname(os.path.abspath('__file__')))\n",
    "print(os.path.dirname(os.path.dirname(os.path.abspath('__file__'))))\n",
    "/Users/zhaojinhui/Desktop/webapp/backend/rmis/jupyter_notebook/__file__\n",
    "/Users/zhaojinhui/Desktop/webapp/backend/rmis/jupyter_notebook\n",
    "/Users/zhaojinhui/Desktop/webapp/backend/rmis\n",
    "'''\n",
    "project = os.path.dirname(os.getcwd())  # get current work directory\n",
    "sys.path.append(project)\n",
    "sys.path.append(os.path.join(project, 'rmis'))\n",
    "# sys.path.append已设置临时环境变量，可以直接调用其中的文件，脚本是外部脚本，只是放在了项目当中而已\n",
    "os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'settings')\n",
    "# 相关数据库配置等都在settings.py文件中\n",
    "# 导入并装载django\n",
    "import django\n",
    "django.setup()\n",
    "# 脚本正文"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>day_of_week</th>\n",
       "      <th>amount</th>\n",
       "      <th>discount</th>\n",
       "      <th>fk_clerk</th>\n",
       "      <th>fk_customer</th>\n",
       "      <th>fk_store</th>\n",
       "      <th>id</th>\n",
       "      <th>interests</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>周一</td>\n",
       "      <td>3141</td>\n",
       "      <td>0</td>\n",
       "      <td>2103</td>\n",
       "      <td>2327</td>\n",
       "      <td>29</td>\n",
       "      <td>88005</td>\n",
       "      <td>1580</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>周二</td>\n",
       "      <td>2679</td>\n",
       "      <td>0</td>\n",
       "      <td>1601</td>\n",
       "      <td>2516</td>\n",
       "      <td>29</td>\n",
       "      <td>88030</td>\n",
       "      <td>1350</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>周三</td>\n",
       "      <td>198</td>\n",
       "      <td>0</td>\n",
       "      <td>522</td>\n",
       "      <td>420</td>\n",
       "      <td>9</td>\n",
       "      <td>17609</td>\n",
       "      <td>100</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>周四</td>\n",
       "      <td>944</td>\n",
       "      <td>0</td>\n",
       "      <td>1062</td>\n",
       "      <td>1368</td>\n",
       "      <td>19</td>\n",
       "      <td>52833</td>\n",
       "      <td>475</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>周五</td>\n",
       "      <td>1544</td>\n",
       "      <td>0</td>\n",
       "      <td>558</td>\n",
       "      <td>1435</td>\n",
       "      <td>23</td>\n",
       "      <td>52842</td>\n",
       "      <td>775</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>周六</td>\n",
       "      <td>4444</td>\n",
       "      <td>0</td>\n",
       "      <td>4708</td>\n",
       "      <td>4640</td>\n",
       "      <td>70</td>\n",
       "      <td>176055</td>\n",
       "      <td>2235</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>周日</td>\n",
       "      <td>1901</td>\n",
       "      <td>0</td>\n",
       "      <td>1561</td>\n",
       "      <td>1414</td>\n",
       "      <td>23</td>\n",
       "      <td>52791</td>\n",
       "      <td>955</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  day_of_week  amount  discount  fk_clerk  fk_customer  fk_store      id  \\\n",
       "0          周一    3141         0      2103         2327        29   88005   \n",
       "1          周二    2679         0      1601         2516        29   88030   \n",
       "2          周三     198         0       522          420         9   17609   \n",
       "3          周四     944         0      1062         1368        19   52833   \n",
       "4          周五    1544         0       558         1435        23   52842   \n",
       "5          周六    4444         0      4708         4640        70  176055   \n",
       "6          周日    1901         0      1561         1414        23   52791   \n",
       "\n",
       "   interests  \n",
       "0       1580  \n",
       "1       1350  \n",
       "2        100  \n",
       "3        475  \n",
       "4        775  \n",
       "5       2235  \n",
       "6        955  "
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from django.forms import model_to_dict\n",
    "from invoice.models import Sale\n",
    "\n",
    "\n",
    "sales = [ model_to_dict(sale) for sale in Sale.objects.filter()[:30] ]\n",
    "df_sale = pd.DataFrame.from_records(sales)\n",
    "\n",
    "# t = pd.Timestamp(\"2019/4/7 22:50:30\")\n",
    "# month_of_year = t.month\n",
    "# day_of_month = t.day\n",
    "# day_of_week = t.dayofweek + 1  #  Mon-Sun 0-6\n",
    "# hour_of_day = t.hour\n",
    "# print( month_of_year, day_of_month, day_of_week, hour_of_day)\n",
    "\n",
    "# [\"created\",\"fk_store\",\"amount\",\"interests\"]\n",
    "df_sale[\"day_of_week\"] = df_sale['created'].apply(lambda cell:cell.dayofweek)\n",
    "df_sale[lambda row:row.fk_store==5]\n",
    "df_sale = df_sale.groupby(\"day_of_week\").sum().sort_index()\n",
    "weekdays = {0: \"周一\", 1: \"周二\", 2: \"周三\", 3: \"周四\", 4: \"周五\", 5: \"周六\", 6: \"周日\"}\n",
    "df_sale = df_sale.reset_index()\n",
    "df_sale[\"day_of_week\"] = df_sale[\"day_of_week\"].apply(lambda x:weekdays[x])\n",
    "df_sale\n",
    "# print(\n",
    "#     df_sale[\"amount\"].to_list()\n",
    "# )\n",
    "# print(\n",
    "#     df_sale.reset_index().loc[:, [\"hour_of_day\", \"amount\"]]\n",
    "# )\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'周一'"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "weekdays[0]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
